
export default class GameData {
    public static LoadScene = "";
    public static isBack = false;
    private static _instance: GameData = null;
    public static get Instance(): GameData {
        if (!this._instance) {
            this.ReadData();
        }
        return this._instance;
    }
    public get Datas() {
        return this.datas;
    }
    public set Datas(value: number[]) {
        this.datas = value;
        GameData.DataSave();
    }
    public get Upgrades(): Map<string, (number | boolean | string)[]> {
        return this.upgrades;
    }

    public set Upgrades(value: Map<string, (number | boolean | string)[]>) {
        this.upgrades = value;
        GameData.DataSave();
    }

    private upgrades = new Map([
        ["assistant", [1400, false, "培训你的店员，更快补充配料"],],
        ["knife", [350, false, "一把更好的刀，帮你更快地切沙威玛"]],
        ["ketchup", [650, false, ""]],
        ["juice", [150, false, ""]],
        ["cake", [650, false, "更好的面饼，提高价格"]],
        ["package", [800, false, ""]],
        ["sandwich", [250, false, "更好的配料，提高价格"]],
        ["price", [3100, false, "更好的包装，提高价格"]],
        ["kobe", [500, false, ""]],
        ["drinksMach", [350, false, ""]],
        ["grill", [250, false, ""]],
        ["fryMach", [1000, false, "炸薯条花太长时间了吗？试试这个！"]],
        ["frenchFry", [1200, false, ""]],
        ["potato", [150, false, "更快地切土豆"]],
        ["4thGuest", [1850, false, "扩展你的餐厅，能够容纳第四名顾客"]]
    ]);

    private datas = [0, 1, 0, 120, 7];

    private jsonUpgrades;

    public static DataSave() {
        GameData.Instance.jsonUpgrades = Object.fromEntries(GameData.Instance.upgrades.entries());
        let json = JSON.stringify(GameData.Instance);
        localStorage.setItem("GameData", json);
        console.log("游戏存档");
    }
    public static ReadData() {
        let name = localStorage.getItem("GameData");
        if (name != "" && name != null && name != undefined) {
            GameData._instance = Object.assign(new GameData(), JSON.parse(name));
            GameData.Instance.Upgrades = new Map(Object.entries(GameData.Instance.jsonUpgrades));
            console.log(GameData.Instance.Upgrades);

        } else {
            GameData._instance = new GameData();
        }
    }

    /**
     * Returns a floating-point random number between min (inclusive) and max (exclusive).
     *
     * @method randomRange
     * @param {number} min
     * @param {number} max
     * @return {number} the random number
     */
    static randomRange(min, max) {
        return Math.random() * (max - min) + min;
    }

    /**
     * Clamps a value between a minimum float and maximum float value.
     *
     * @method clamp
     * @param {number} val
     * @param {number} min
     * @param {number} max
     * @return {number}
     */
    static clamp(val, min, max) {
        console.log(val < min);
        console.log(max < val);
        var num = val < min ? min : (val > max ? max : val);
        console.log(num);
        return val < min ? min : (val > max ? max : val);
    }
}

export enum Upgrades {
    assistant = "assistant",
    knife = "knife",
    ketchup = "ketchup",
    juice = "juice",
    cake = "cake",
    package = "package",
    sandwich = "sandwich",
    price = "price",
    kobe = "kobe",
    drinksMach = "drinksMach",
    grill = "grill",
    fryMach = "fryMach",
    frenchFry = "frenchFry",
    potato = "potato",
    FthGuest = "4thGuest"
}
